/*
 * Copyright (c) 2011-2013, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.alg.feature.disparity.impl;

import boofcv.misc.AutoTypeImage;
import boofcv.misc.CodeGeneratorBase;

import java.io.FileNotFoundException;

/**
 * @author Peter Abeles
 */
public class GenerateSelectRectBasicWta extends CodeGeneratorBase {

	String typeDisparity;
	String dataType;
	String dataAbr;
	String sumType;

	@Override
	public void generate() throws FileNotFoundException {
		createFile(false,AutoTypeImage.U8);
		createFile(true,AutoTypeImage.U8);
	}

	public void createFile( boolean isFloat , AutoTypeImage disparity ) throws FileNotFoundException {
		typeDisparity = disparity.getSingleBandName();
		dataType = disparity.getDataType();
		if( isFloat ) {
			sumType = "float";
			dataAbr = "F32";
		} else {
			sumType = "int";
			dataAbr = "S32";
		}

		setOutputFile("ImplSelectRectBasicWta_"+dataAbr+"_"+disparity.getAbbreviatedType());

		printPreamble();
	}

	private void printPreamble() {
		out.print("import boofcv.alg.feature.disparity.SelectRectBasicWta;\n" +
				"import boofcv.struct.image."+typeDisparity+";\n" +
				"\n" +
				"/**\n" +
				" * <p>\n" +
				" * Implementation of {@link SelectRectBasicWta} for scores of type "+dataAbr+".\n" +
				" * </p>\n" +
				" *\n" +
				" * <p>\n" +
				" * DO NOT MODIFY. Generated by {@link GenerateSelectRectBasicWta}.\n" +
				" * </p>\n" +
				" *\n" +
				" * @author Peter Abeles\n" +
				" */\n" +
				"public class "+className+" extends SelectRectBasicWta<"+sumType+"[],"+typeDisparity+">\n" +
				"{\n" +
				"\t@Override\n" +
				"\tpublic void process(int row, "+sumType+"[] scores) {\n" +
				"\n" +
				"\t\tint indexDisparity = imageDisparity.startIndex + row*imageDisparity.stride + radiusX + minDisparity;\n" +
				"\n" +
				"\t\tfor( int col = minDisparity; col <= imageWidth-regionWidth; col++ ) {\n" +
				"\t\t\t// make sure the disparity search doesn't go outside the image border\n" +
				"\t\t\tint localMax = maxDisparityAtColumnL2R(col);\n" +
				"\n" +
				"\t\t\tint indexScore = col-minDisparity;\n" +
				"\n" +
				"\t\t\tint bestDisparity = 0;\n" +
				"\t\t\t"+sumType+" scoreBest = scores[indexScore];\n" +
				"\t\t\tindexScore += imageWidth;\n" +
				"\n" +
				"\t\t\tfor( int i = 1; i < localMax; i++ ,indexScore += imageWidth) {\n" +
				"\t\t\t\t"+sumType+" s = scores[indexScore];\n" +
				"\t\t\t\tif( s < scoreBest ) {\n" +
				"\t\t\t\t\tscoreBest = s;\n" +
				"\t\t\t\t\tbestDisparity = i;\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\n" +
				"\t\t\timageDisparity.data[indexDisparity++] = ("+dataType+")bestDisparity;\n" +
				"\t\t}\n" +
				"\t}\n" +
				"\n" +
				"\t@Override\n" +
				"\tpublic Class<"+typeDisparity+"> getDisparityType() {\n" +
				"\t\treturn "+typeDisparity+".class;\n" +
				"\t}\n" +
				"}");
	}

	public static void main( String args[] ) throws FileNotFoundException {
		GenerateSelectRectBasicWta gen = new GenerateSelectRectBasicWta();

		gen.generate();
	}
}
